package com.moxi.controller;

import com.moxi.model.User;
import com.moxi.service.UserService;
import com.moxi.util.Constant;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    //用户信息页面
    @RequestMapping("/admin/userManage_{pageCurrent}_{pageSize}_{pageCount}")
    public String userManage(User user, @PathVariable Integer pageCurrent, @PathVariable Integer pageSize, @PathVariable Integer pageCount, Model model){

        //判断
        if(pageSize == 0) pageSize = 10;
        if(pageCurrent == 0) pageCurrent = 1;
        int rows = userService.count(user);
        if(pageCount == 0) pageCount = rows%pageSize == 0 ? (rows/pageSize) : (rows/pageSize) + 1;


        //查询
        user.setStart((pageCurrent - 1)*pageSize);
        user.setEnd(pageSize);
        if(user.getOrderBy()==null){user.setOrderBy(Constant.OrderByAddDateDesc);}
        List<User> userList = userService.list(user);

        //输出
        model.addAttribute("userList",userList);
        return "user/userManage";
    }

    /**
     * 跳转添加用户信息
     * @param model
     * @param user
     * @return
     */
    @GetMapping("/admin/userEdit")
    public String newsEditGet(Model model, User user) {
        if(user.getId()!=0){
            User userU = userService.findById(user);
            model.addAttribute("user",userU);
        }
        return "user/userAdd";
    }

    /**
     * 提交添加用户信息
     * @param model
     * @param user
     * @param imageFile
     * @param httpSession
     * @return
     */
    @PostMapping("/admin/userEdit")
    public String newsEditPost(Model model, User user, @RequestParam MultipartFile[] imageFile, HttpSession httpSession) {
        for (MultipartFile file : imageFile) {
            if (file.isEmpty()) {
                System.out.println("文件未上传");
            } else {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                Random random = new Random();
                Date date = new Date();
                String strDate = sdf.format(date);
                String fileName = strDate + "_" + random.nextInt(1000) + file.getOriginalFilename().substring(file.getOriginalFilename().indexOf("."),file.getOriginalFilename().length());
                String realPath = httpSession.getServletContext().getRealPath("/userfiles");
                System.out.println("realPath : "+realPath);
                try {
                    FileUtils.copyInputStreamToFile(file.getInputStream(),new File(realPath, fileName));
                    user.setHeadPicture("/userfiles/"+fileName);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if(user.getId()!=0){
            userService.update(user);
        } else {
            userService.insert(user);
        }
        return "redirect:userManage_0_0_0";
    }


}
